package com.webchat.act.repository.dao;

import com.webchat.act.repository.entity.ResourceBehaviorEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IResourceBehaviorDAO extends JpaSpecificationExecutor<ResourceBehaviorEntity>,
                                              JpaRepository<ResourceBehaviorEntity, Long> {

    ResourceBehaviorEntity findByUserIdAndBehaviorTypeAndResourceTypeAndResourceIndexAndStatusTrue(
            String userId, String behaviorType, String resourceType, Long resourceIndex);


    Long countByBehaviorTypeAndResourceTypeAndResourceIndexAndStatusTrue(
            String behaviorType, String resourceType, Long resourceIndex);


    @Query(value = "select rbe.resourceIndex, rbe.updateDate " +
                   "from ResourceBehaviorEntity rbe " +
                   "where rbe.userId = :userId and rbe.status = true and rbe.behaviorType = :behaviorType" +
                   " and rbe.resourceType = :resourceType")
    List<ResourceBehaviorEntity> findAllByUserBehaviorResource(
            String userId, String behaviorType, String resourceType);

    @Query(value = "select rbe.userId, rbe.updateDate " +
            "from ResourceBehaviorEntity rbe " +
            "where rbe.status = true and rbe.behaviorType = :behaviorType " +
            " and rbe.resourceType = :resourceType and rbe.resourceIndex = :resourceId")
    List<ResourceBehaviorEntity> findAllByResourceBehaviorUser(
            Long resourceId, String behaviorType, String resourceType);

}
